<?php

namespace app\common\library;

use GuzzleHttp\Client;

/**
 * 用友
 */
class Yyou
{
    public $appId = 'c69a44d6d7694ba8a49155d4247e6c99';
    public $appSecret = '5c228177637f404e84517819dad143a0';
    public $params;
    public $url = 'https://open.xinfushe.com';
    public function __construct($data)
    {
        $this->params = [
            'json' =>
            [
                'appId' => $this->appId,
                'data' => $data,
                'sign' => $this->sign($data),
            ]
        ];
    }
    public function sign($params)
    {
        uksort($params, 'strcmp');
        $str = '';
        foreach ($params as $key => $val) {
            $str .= "{$key}={$val},";
        }
        $sign = md5(urlencode($str . "secret=" . $this->appSecret));
        return $sign;
    }
    /**
     * Json方式 查询账号
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function getAccountJson()
    {
        $result = new client();
        $result = $result->post($this->url . '/api/R_ACCOUNT_02', $this->params);
        $body = $result->getBody()->getContents();
        $data = json_decode($body, true);

        if ($data['code'] != 000000) {
            exception($data['mesg']);
        }

        return $data['data'];
    }
    /**
     * Json方式 创建账号
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function getAddAccountJson()
    {
        $result = new client();

        $result = $result->post($this->url . '/api/C_ACCOUNT_01', $this->params);
        $body = $result->getBody()->getContents();
        $data = json_decode($body, true);
        if ($data['code'] != 000000) {
            exception($data['mesg']);
        }
        return $data;
    }

    /**
     * Json方式 查询人员信息
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function getAccountInfoJson()
    {
        $result = new client();
        $result = $result->post($this->url . '/api/R_EMPLOYEE_01', $this->params);
        $body = $result->getBody()->getContents();
        $data = json_decode($body, true);
        return $data;
    }

    /**
     * Json方式 获取待办中心
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function getCenterJson()
    {
        $result = new client();
        $result = $result->post($this->url . '/api/TO_DO_01', $this->params);
        $body = $result->getBody()->getContents();
        $data = json_decode($body, true);
        if ($data['state'] != 200) {
            exception($data['message']);
        }
        return $data;
    }
    /**
     * Json方式 发布需求
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function addRequirementJson()
    {
        $result = new client();
        $result = $result->post($this->url . '/api/R_SOCIAL_01', $this->params);
        $body = $result->getBody()->getContents();
        $data = json_decode($body, true);
        if ($data['state'] != 200) {
            exception($data['message']);
        }
        return $data;
    }
    /**
     * Json方式 新建任务
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function addTaskJson()
    {
        $result = new client();

        $result = $result->post($this->url . '/api/R_SOCIAL_03', $this->params);
        $body = $result->getBody()->getContents();
        $data = json_decode($body, true);
        if ($data['state'] != 200) {
            exception($data['message']);
        }
        return $data;
    }

    /**
     * Json方式 验收任务
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function acceptanceTaskJson()
    {
        $result = new client();

        $result = $result->post($this->url . '/api/R_SOCIAL_05', $this->params);
     
        $body = $result->getBody()->getContents();
        $data = json_decode($body, true);
        if ($data['state'] != 200) {
            exception($data['message']);
        }
        return $data;
    }
    /**
     * Json方式 解绑自然人信息
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function unbindNaturalPerson()
    {
        $result = new client();

        $result = $result->post($this->url . '/api/U_UNBIND_01', $this->params);
        $body = $result->getBody()->getContents();
        $data = json_decode($body, true);
        if ($data['state'] != 200) {
            exception($data['message']);
        }
        return $data;
    }
}
